<template>
  <div class="student-management">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">学生管理</h1>
        <p class="page-subtitle">管理系统中的所有学生用户</p>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="showCreateDialog">
          <el-icon><Plus /></el-icon>
          添加学生
        </el-button>
        <el-button @click="exportData">
          <el-icon><Download /></el-icon>
          导出数据
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :xs="12" :sm="6">
        <el-card class="stats-card">
          <div class="stats-content">
            <div class="stats-info">
              <div class="stats-number">{{ stats.total }}</div>
              <div class="stats-label">总学生数</div>
            </div>
            <el-icon class="stats-icon"><User /></el-icon>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stats-card">
          <div class="stats-content">
            <div class="stats-info">
              <div class="stats-number">{{ stats.active }}</div>
              <div class="stats-label">活跃学生</div>
            </div>
            <el-icon class="stats-icon active"><UserFilled /></el-icon>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stats-card">
          <div class="stats-content">
            <div class="stats-info">
              <div class="stats-number">{{ stats.newToday }}</div>
              <div class="stats-label">今日新增</div>
            </div>
            <el-icon class="stats-icon new"><Plus /></el-icon>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6">
        <el-card class="stats-card">
          <div class="stats-content">
            <div class="stats-info">
              <div class="stats-number">{{ stats.disabled }}</div>
              <div class="stats-label">已禁用</div>
            </div>
            <el-icon class="stats-icon disabled"><Lock /></el-icon>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 搜索和筛选 -->
    <el-card class="filter-card">
      <el-form :model="filters" :inline="true" class="filter-form">
        <el-form-item label="关键词">
          <el-input
            v-model="filters.keyword"
            placeholder="搜索学号、姓名、邮箱"
            clearable
            style="width: 200px"
            @keyup.enter="loadStudents"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item label="学院">
          <el-select v-model="filters.college" placeholder="选择学院" clearable style="width: 150px">
            <el-option label="计算机学院" value="computer" />
            <el-option label="电子工程学院" value="electronic" />
            <el-option label="机械工程学院" value="mechanical" />
            <el-option label="经济管理学院" value="economics" />
            <el-option label="外国语学院" value="foreign" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="年级">
          <el-select v-model="filters.grade" placeholder="选择年级" clearable style="width: 120px">
            <el-option label="2024级" value="2024" />
            <el-option label="2023级" value="2023" />
            <el-option label="2022级" value="2022" />
            <el-option label="2021级" value="2021" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="状态">
          <el-select v-model="filters.status" placeholder="选择状态" clearable style="width: 120px">
            <el-option label="正常" value="active" />
            <el-option label="禁用" value="disabled" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="注册时间">
          <el-date-picker
            v-model="filters.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            style="width: 240px"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="loadStudents">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetFilters">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 学生列表 -->
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <div class="header-title">
            <span>学生列表</span>
            <span class="total-count">（共 {{ pagination.total }} 条）</span>
          </div>
          <div class="header-actions">
            <el-button 
              type="success" 
              size="small" 
              :disabled="selectedStudents.length === 0"
              @click="batchEnable"
            >
              批量启用
            </el-button>
            <el-button 
              type="warning" 
              size="small" 
              :disabled="selectedStudents.length === 0"
              @click="batchDisable"
            >
              批量禁用
            </el-button>
            <el-button 
              type="danger" 
              size="small" 
              :disabled="selectedStudents.length === 0"
              @click="batchDelete"
            >
              批量删除
            </el-button>
          </div>
        </div>
      </template>
      
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="5" animated />
      </div>
      
      <div v-else-if="students.length === 0" class="empty-container">
        <el-empty description="暂无学生数据" />
      </div>
      
      <div v-else>
        <el-table
          :data="students"
          @selection-change="handleSelectionChange"
          stripe
          style="width: 100%"
        >
          <el-table-column type="selection" width="55" />
          
          <el-table-column label="头像" width="80">
            <template #default="{ row }">
              <el-avatar :src="row.avatar" :size="40">
                {{ row.realName?.charAt(0) || row.username?.charAt(0) }}
              </el-avatar>
            </template>
          </el-table-column>
          
          <el-table-column prop="studentId" label="学号" width="120" />
          
          <el-table-column prop="username" label="用户名" width="120" />
          
          <el-table-column prop="realName" label="真实姓名" width="100" />
          
          <el-table-column prop="email" label="邮箱" width="180" show-overflow-tooltip />
          
          <el-table-column prop="phone" label="联系电话" width="120" />
          
          <el-table-column label="学院/专业" width="150">
            <template #default="{ row }">
              <div class="college-info">
                <div class="college">{{ row.college }}</div>
                <div class="major">{{ row.major }}</div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column prop="grade" label="年级" width="80" />
          
          <el-table-column label="活动参与" width="100">
            <template #default="{ row }">
              <div class="activity-stats">
                <div class="stat-item">
                  <span class="stat-label">参与:</span>
                  <span class="stat-value">{{ row.activityCount || 0 }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          
          <el-table-column label="状态" width="80">
            <template #default="{ row }">
              <el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
                {{ row.status === 'active' ? '正常' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          
          <el-table-column prop="createdAt" label="注册时间" width="120">
            <template #default="{ row }">
              {{ formatDate(row.createdAt, 'YYYY-MM-DD') }}
            </template>
          </el-table-column>
          
          <el-table-column prop="lastLoginAt" label="最后登录" width="120">
            <template #default="{ row }">
              {{ row.lastLoginAt ? formatDate(row.lastLoginAt, 'MM-DD HH:mm') : '从未登录' }}
            </template>
          </el-table-column>
          
          <el-table-column label="操作" width="200" fixed="right">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="viewDetail(row)">
                查看
              </el-button>
              <el-button type="text" size="small" @click="editStudent(row)">
                编辑
              </el-button>
              <el-button 
                type="text" 
                size="small" 
                @click="toggleStatus(row)"
              >
                {{ row.status === 'active' ? '禁用' : '启用' }}
              </el-button>
              <el-button type="text" size="small" @click="resetPassword(row)">
                重置密码
              </el-button>
              <el-button type="text" size="small" @click="deleteStudent(row)" class="danger">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- 分页 -->
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="pagination.page"
            v-model:page-size="pagination.size"
            :total="pagination.total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="loadStudents"
            @current-change="loadStudents"
          />
        </div>
      </div>
    </el-card>

    <!-- 创建/编辑学生对话框 -->
    <el-dialog
      v-model="studentDialogVisible"
      :title="studentMode === 'create' ? '添加学生' : '编辑学生'"
      width="600px"
      @close="resetStudentForm"
    >
      <el-form
        ref="studentFormRef"
        :model="studentForm"
        :rules="studentRules"
        label-width="100px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学号" prop="studentId">
              <el-input v-model="studentForm.studentId" placeholder="请输入学号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="studentForm.username" placeholder="请输入用户名" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="真实姓名" prop="realName">
              <el-input v-model="studentForm.realName" placeholder="请输入真实姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="studentForm.gender" placeholder="选择性别">
                <el-option label="男" value="male" />
                <el-option label="女" value="female" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="studentForm.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="studentForm.phone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="学院" prop="college">
              <el-select v-model="studentForm.college" placeholder="选择学院">
                <el-option label="计算机学院" value="computer" />
                <el-option label="电子工程学院" value="electronic" />
                <el-option label="机械工程学院" value="mechanical" />
                <el-option label="经济管理学院" value="economics" />
                <el-option label="外国语学院" value="foreign" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="专业" prop="major">
              <el-input v-model="studentForm.major" placeholder="请输入专业" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="年级" prop="grade">
              <el-select v-model="studentForm.grade" placeholder="选择年级">
                <el-option label="2024级" value="2024" />
                <el-option label="2023级" value="2023" />
                <el-option label="2022级" value="2022" />
                <el-option label="2021级" value="2021" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班级" prop="class">
              <el-input v-model="studentForm.class" placeholder="请输入班级" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item v-if="studentMode === 'create'" label="初始密码" prop="password">
          <el-input 
            v-model="studentForm.password" 
            type="password" 
            placeholder="请输入初始密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="studentForm.status">
            <el-radio label="active">正常</el-radio>
            <el-radio label="disabled">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="studentDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitStudent" :loading="submittingStudent">
          {{ studentMode === 'create' ? '创建' : '保存' }}
        </el-button>
      </template>
    </el-dialog>

    <!-- 学生详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="学生详情"
      width="800px"
    >
      <div v-if="currentStudent" class="student-detail">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="头像">
            <el-avatar :src="currentStudent.avatar" :size="60">
              {{ currentStudent.realName?.charAt(0) || currentStudent.username?.charAt(0) }}
            </el-avatar>
          </el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="currentStudent.status === 'active' ? 'success' : 'danger'">
              {{ currentStudent.status === 'active' ? '正常' : '禁用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="学号">{{ currentStudent.studentId }}</el-descriptions-item>
          <el-descriptions-item label="用户名">{{ currentStudent.username }}</el-descriptions-item>
          <el-descriptions-item label="真实姓名">{{ currentStudent.realName }}</el-descriptions-item>
          <el-descriptions-item label="性别">{{ currentStudent.gender === 'male' ? '男' : '女' }}</el-descriptions-item>
          <el-descriptions-item label="邮箱">{{ currentStudent.email }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ currentStudent.phone }}</el-descriptions-item>
          <el-descriptions-item label="学院">{{ currentStudent.college }}</el-descriptions-item>
          <el-descriptions-item label="专业">{{ currentStudent.major }}</el-descriptions-item>
          <el-descriptions-item label="年级">{{ currentStudent.grade }}</el-descriptions-item>
          <el-descriptions-item label="班级">{{ currentStudent.class }}</el-descriptions-item>
          <el-descriptions-item label="注册时间">
            {{ formatDate(currentStudent.createdAt, 'YYYY-MM-DD HH:mm:ss') }}
          </el-descriptions-item>
          <el-descriptions-item label="最后登录">
            {{ currentStudent.lastLoginAt ? formatDate(currentStudent.lastLoginAt, 'YYYY-MM-DD HH:mm:ss') : '从未登录' }}
          </el-descriptions-item>
        </el-descriptions>
        
        <div class="activity-history">
          <h4>活动参与历史</h4>
          <el-table :data="currentStudent.activities || []" size="small">
            <el-table-column prop="title" label="活动名称" />
            <el-table-column prop="status" label="状态">
              <template #default="{ row }">
                <el-tag :type="getRegistrationStatusType(row.status)" size="small">
                  {{ getRegistrationStatusText(row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="registeredAt" label="报名时间">
              <template #default="{ row }">
                {{ formatDate(row.registeredAt, 'YYYY-MM-DD') }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { formatDate } from '@/utils'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '@/utils/api'
import {
  Plus,
  Download,
  User,
  UserFilled,
  Lock,
  Search,
  Refresh
} from '@element-plus/icons-vue'

const router = useRouter()

const loading = ref(false)
const studentDialogVisible = ref(false)
const detailDialogVisible = ref(false)
const studentMode = ref('create')
const submittingStudent = ref(false)
const studentFormRef = ref()

const stats = reactive({
  total: 0,
  active: 0,
  newToday: 0,
  disabled: 0
})

const filters = reactive({
  keyword: '',
  college: '',
  grade: '',
  status: '',
  dateRange: null
})

const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

const students = ref([])
const selectedStudents = ref([])
const currentStudent = ref(null)

const studentForm = reactive({
  id: null,
  studentId: '',
  username: '',
  realName: '',
  gender: '',
  email: '',
  phone: '',
  college: '',
  major: '',
  grade: '',
  class: '',
  password: '',
  status: 'active'
})

const studentRules = {
  studentId: [
    { required: true, message: '请输入学号', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' }
  ],
  college: [
    { required: true, message: '请选择学院', trigger: 'change' }
  ],
  major: [
    { required: true, message: '请输入专业', trigger: 'blur' }
  ],
  grade: [
    { required: true, message: '请选择年级', trigger: 'change' }
  ],
  password: [
    { required: true, message: '请输入初始密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const getRegistrationStatusType = (status) => {
  const typeMap = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger',
    cancelled: 'info'
  }
  return typeMap[status] || ''
}

const getRegistrationStatusText = (status) => {
  const textMap = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝',
    cancelled: '已取消'
  }
  return textMap[status] || status
}

const loadStats = async () => {
  try {
    const response = await api.get('/admin/students/stats')
    const data = response.data.data
    
    Object.assign(stats, data)
    
  } catch (error) {
    console.error('加载统计数据失败:', error)
  }
}

const loadStudents = async () => {
  try {
    loading.value = true
    
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...filters
    }
    
    if (filters.dateRange && filters.dateRange.length === 2) {
      params.startDate = filters.dateRange[0]
      params.endDate = filters.dateRange[1]
    }
    
    const response = await api.get('/admin/students', { params })
    const data = response.data.data
    
    students.value = data.list || []
    pagination.total = data.total || 0
    
  } catch (error) {
    console.error('加载学生列表失败:', error)
    ElMessage.error('加载学生列表失败')
  } finally {
    loading.value = false
  }
}

const resetFilters = () => {
  Object.assign(filters, {
    keyword: '',
    college: '',
    grade: '',
    status: '',
    dateRange: null
  })
  pagination.page = 1
  loadStudents()
}

const handleSelectionChange = (selection) => {
  selectedStudents.value = selection
}

const showCreateDialog = () => {
  studentMode.value = 'create'
  resetStudentForm()
  studentDialogVisible.value = true
}

const editStudent = (student) => {
  studentMode.value = 'edit'
  Object.assign(studentForm, {
    id: student.id,
    studentId: student.studentId,
    username: student.username,
    realName: student.realName,
    gender: student.gender,
    email: student.email,
    phone: student.phone,
    college: student.college,
    major: student.major,
    grade: student.grade,
    class: student.class,
    status: student.status
  })
  studentDialogVisible.value = true
}

const resetStudentForm = () => {
  Object.assign(studentForm, {
    id: null,
    studentId: '',
    username: '',
    realName: '',
    gender: '',
    email: '',
    phone: '',
    college: '',
    major: '',
    grade: '',
    class: '',
    password: '',
    status: 'active'
  })
  if (studentFormRef.value) {
    studentFormRef.value.resetFields()
  }
}

const submitStudent = async () => {
  try {
    await studentFormRef.value.validate()
    
    submittingStudent.value = true
    
    if (studentMode.value === 'create') {
      await api.post('/admin/students', studentForm)
      ElMessage.success('学生创建成功')
    } else {
      await api.put(`/admin/students/${studentForm.id}`, studentForm)
      ElMessage.success('学生信息更新成功')
    }
    
    studentDialogVisible.value = false
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else {
      ElMessage.error('操作失败')
    }
  } finally {
    submittingStudent.value = false
  }
}

const viewDetail = async (student) => {
  try {
    const response = await api.get(`/admin/students/${student.id}`)
    currentStudent.value = response.data.data
    detailDialogVisible.value = true
  } catch (error) {
    ElMessage.error('获取学生详情失败')
  }
}

const toggleStatus = async (student) => {
  try {
    const action = student.status === 'active' ? '禁用' : '启用'
    await ElMessageBox.confirm(
      `确定要${action}学生 ${student.realName} 吗？`,
      `确认${action}`,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const newStatus = student.status === 'active' ? 'disabled' : 'active'
    await api.put(`/admin/students/${student.id}/status`, { status: newStatus })
    
    ElMessage.success(`${action}成功`)
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('操作失败')
    }
  }
}

const resetPassword = async (student) => {
  try {
    await ElMessageBox.confirm(
      `确定要重置学生 ${student.realName} 的密码吗？重置后密码将变为默认密码。`,
      '确认重置密码',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await api.put(`/admin/students/${student.id}/reset-password`)
    ElMessage.success('密码重置成功')
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('密码重置失败')
    }
  }
}

const deleteStudent = async (student) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除学生 ${student.realName} 吗？此操作不可恢复。`,
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    await api.delete(`/admin/students/${student.id}`)
    ElMessage.success('删除成功')
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const batchEnable = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要启用选中的 ${selectedStudents.value.length} 个学生吗？`,
      '确认批量启用',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const ids = selectedStudents.value.map(student => student.id)
    await api.put('/admin/students/batch-status', { ids, status: 'active' })
    
    ElMessage.success('批量启用成功')
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('批量启用失败')
    }
  }
}

const batchDisable = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要禁用选中的 ${selectedStudents.value.length} 个学生吗？`,
      '确认批量禁用',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const ids = selectedStudents.value.map(student => student.id)
    await api.put('/admin/students/batch-status', { ids, status: 'disabled' })
    
    ElMessage.success('批量禁用成功')
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('批量禁用失败')
    }
  }
}

const batchDelete = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要删除选中的 ${selectedStudents.value.length} 个学生吗？此操作不可恢复。`,
      '确认批量删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const ids = selectedStudents.value.map(student => student.id)
    await api.delete('/admin/students/batch', { data: { ids } })
    
    ElMessage.success('批量删除成功')
    loadStudents()
    loadStats()
    
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('批量删除失败')
    }
  }
}

const exportData = async () => {
  try {
    const response = await api.get('/admin/students/export', {
      params: filters,
      responseType: 'blob'
    })
    
    const blob = new Blob([response.data], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    })
    
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = `学生数据_${formatDate(new Date(), 'YYYY-MM-DD')}.xlsx`
    link.click()
    
    window.URL.revokeObjectURL(url)
    ElMessage.success('导出成功')
    
  } catch (error) {
    ElMessage.error('导出失败')
  }
}

onMounted(() => {
  loadStats()
  loadStudents()
})
</script>

<style scoped>
.student-management {
  max-width: 1400px;
  margin: 0 auto;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 8px;
  color: white;
}

.header-content {
  flex: 1;
}

.page-title {
  font-size: 24px;
  font-weight: 700;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 12px;
  margin-left: 20px;
}

.stats-row {
  margin-bottom: 24px;
}

.stats-card {
  cursor: pointer;
  transition: transform 0.3s ease;
}

.stats-card:hover {
  transform: translateY(-2px);
}

.stats-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.stats-info {
  flex: 1;
}

.stats-number {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 4px;
}

.stats-label {
  font-size: 14px;
  color: #909399;
}

.stats-icon {
  font-size: 28px;
  opacity: 0.3;
  color: #409eff;
}

.stats-icon.active {
  color: #67c23a;
}

.stats-icon.new {
  color: #e6a23c;
}

.stats-icon.disabled {
  color: #f56c6c;
}

.filter-card,
.table-card {
  margin-bottom: 20px;
}

.filter-form {
  margin-bottom: 0;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #303133;
}

.total-count {
  font-size: 12px;
  color: #909399;
  font-weight: normal;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.loading-container,
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.college-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.college {
  font-size: 13px;
  color: #303133;
}

.major {
  font-size: 12px;
  color: #909399;
}

.activity-stats {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

.stat-label {
  color: #909399;
}

.stat-value {
  color: #409eff;
  font-weight: 600;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.student-detail {
  max-height: 600px;
  overflow-y: auto;
}

.activity-history {
  margin-top: 24px;
}

.activity-history h4 {
  margin: 0 0 16px 0;
  color: #303133;
}

.danger {
  color: #f56c6c !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }
  
  .header-actions {
    margin-left: 0;
    flex-direction: column;
  }
  
  .filter-form {
    flex-direction: column;
  }
  
  .filter-form .el-form-item {
    margin-right: 0;
    margin-bottom: 16px;
  }
  
  .table-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
  
  .header-actions {
    flex-direction: row;
    flex-wrap: wrap;
  }
}
</style>